<%@ Import Namespace="System.Diagnostics" %>


<html>

<script language="VB" runat="server">

  Sub Page_Load(Sender As Object, e as EventArgs)

    If Page.IsPostBack Then
      ' does the category already exist
      If PerformanceCounterCategory.Exists("Wrox") Then
        ' increment the postback counter
        IncrementCounter("Wrox", "PostBack")
      End If
    End If

  End Sub


  Sub IncrementCounter(Category As String, Counter As String)

    If PerformanceCounterCategory.Exists("Wrox") Then
      Dim pc As New PerformanceCounter(Category, Counter, False)
      pc.Increment()
    End If
        
  End Sub
  
  
  Sub DecrementCounter(Category As String, Counter As String)

    If PerformanceCounterCategory.Exists("Wrox") Then
      Dim pc As New PerformanceCounter(Category, Counter, False)
      
      If pc.rawValue > 0 Then
        pc.Decrement()
      End If
    End If
        
  End Sub


  Sub CreateCounters_Click(Sender As Object, e as EventArgs)
  
    ' only create the counters if they don't already exist
    If Not PerformanceCounterCategory.Exists("Wrox") Then
      ' set up the counter details
      Dim ccdc As New CounterCreationDataCollection()

      ccdc.Add(New CounterCreationData("Counter1", "Counter 1",         PerformanceCounterType.NumberOfItems32))
      ccdc.Add(New CounterCreationData("Counter2", "Counter 2",         PerformanceCounterType.NumberOfItems32))
      ccdc.Add(New CounterCreationData("Counter3", "Counter 3",         PerformanceCounterType.NumberOfItems32))
      ccdc.Add(New CounterCreationData("PostBack", "Postback occurred", PerformanceCounterType.NumberOfItems32))

      PerformanceCounterCategory.Create("Wrox", "Wrox Press Professional ASP.NET Samples", ccdc)
    End If

    ShowCounterValues()
    
  End Sub
  
  Sub CreateCounters2_Click(Sender As Object, e as EventArgs)
  	dim pc as new PerformanceCounterCategory("Wrox")
		Response.Write("foo=" & pc.CounterExists("Counter1").ToString())
		
  End Sub


  Sub ProcessCounter_Click(Sender As Object, e as CommandEventArgs)

    If PerformanceCounterCategory.Exists("Wrox") Then
    
      ' The CommandName contains what we want to do (Increment/Decrement), and
      ' the CommandArgument contains the name of the counter
      If e.CommandName = "Increment" Then
        IncrementCounter("Wrox", e.CommandArgument)
      Else
        DecrementCounter("Wrox", e.CommandArgument)
      End If

      ShowCounterValues()
    End If

  End Sub
  
  
  Sub DeleteCounters_Click(Sender As Object, e as EventArgs)
  
    ' delete all counters and the catageory
    If PerformanceCounterCategory.Exists("Wrox") Then
      Dim cat As New PerformanceCounterCategory("Wrox")
      Dim pc  As New PerformanceCounter
      For Each pc In cat.GetCounters()
      	pc.CloseSharedResources()
      Next
      PerformanceCounterCategory.Delete("Wrox")
      Page.Controls.Remove(CounterValues)
    End If
  
  End Sub


  ' create a table showing the counter values
  Sub ShowCounterValues()
  
    If PerformanceCounterCategory.Exists("Wrox") Then
      Dim cat As New PerformanceCounterCategory("Wrox")
      Dim pc  As New PerformanceCounter
      Dim s   As String
      Dim tr  As TableRow
      Dim tcc As TableCell
      Dim tcv As TableCell
      Dim tct As TableCell
      Dim img As HtmlImage

      ' Construct a table row for each counter
      For Each pc In cat.GetCounters()
        ' cell for text
        tcc = New TableCell
        tcc.Text = pc.CounterHelp
        tcc.Width = Unit.Percentage(20)
        
        ' text value
        tct = New TableCell
        tct.Text = pc.rawValue
        tct.Width = Unit.Percentage(5)
        
        ' bar graph and cell containing it
        img = New HtmlImage
        img.Src = "RedPixel.gif"
        img.Width = pc.RawValue
        img.Height = 20
        tcv = New TableCell
        tcv.Controls.Add(img)
        
        ' add cells to row, and rows to table
        tr = New TableRow
        tr.Cells.Add(tcc)
        tr.Cells.Add(tct)
        tr.Cells.Add(tcv)
        CounterValues.Rows.Add(tr)
      Next
    End If

  End Sub

</script>

<form runat="server">

  <asp:Button Text="Create Counters" onclick="CreateCounters_Click" runat="server"/>
  &nbsp;
  <asp:Button Text="Create Counters 2" onclick="CreateCounters2_Click" runat="server"/>
  &nbsp;
  <asp:Button Text="Delete Counters" onclick="DeleteCounters_Click" runat="server"/>
  <p/>
  
  <table cellspacing="10">
    <tr>
      <td align="center">Counter 1</td>
      <td align="center">Counter 2</td>
      <td align="center">Counter 3</td>
    </tr>
    <tr>
      <td>
        <asp:Button CommandName="Increment" CommandArgument="Counter1" Text="Increment"
             onCommand="ProcessCounter_Click" runat="server"/>
        <asp:Button CommandName="Decrement" CommandArgument="Counter1" Text="Decrement"
             onCommand="ProcessCounter_Click" runat="server"/>
      </td>
      <td>
        <asp:Button CommandName="Increment" CommandArgument="Counter2" Text="Increment"
             onCommand="ProcessCounter_Click" runat="server"/>
        <asp:Button CommandName="Decrement" CommandArgument="Counter2" Text="Decrement"
             onCommand="ProcessCounter_Click" runat="server"/>
      </td>
      <td>
        <asp:Button CommandName="Increment" CommandArgument="Counter3" Text="Increment"
             onCommand="ProcessCounter_Click" runat="server"/>
        <asp:Button CommandName="Decrement" CommandArgument="Counter4" Text="Decrement"
             onCommand="ProcessCounter_Click" runat="server"/>
      </td>
    </tr>
  </table>
        
  <p/>

  <asp:Table id="CounterValues" width="75%" runat="server">
    <asp:TableRow>
      <asp:TableHeaderCell HorizontalAlign="Left">Counter</asp:TableHeaderCell>
      <asp:TableHeaderCell HorizontalAlign="Left">Value</asp:TableHeaderCell>
      <asp:TableHeaderCell HorizontalAlign="Left">Value</asp:TableHeaderCell>
    </asp:TableRow>
  </asp:table>
  
</form>

</html>